package com.bee.common.tools.regex;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RestfulRegexUtils {

    private static String anyPattern="(?<=\\{)(.+?)(?=\\})";

    private static String LongParttern="\\d+";

    /**
     * 将restful的参数转化为*
     * @param uri
     * @return
     */
    public static String reverseParametersRegex(String uri){
       return RestfulRegexUtils.Regex(uri,anyPattern,"*");
    }

    private static String Regex(String uri,String parttern,String repalce){
        List<String> parameters=new ArrayList<String>();
        Pattern pattern = Pattern.compile(parttern);
        Matcher matcher = pattern.matcher(uri);
        while(matcher.find()){
            parameters.add(matcher.group());
        }
        for (String parameter : parameters) {
            uri=uri.replace(parameter,repalce);
        }
        return uri;
    }

    /**
     * 将restful的整型参数转化为*
     * @param uri
     * @return
     */
    public static String obverseParametersRegex(String uri){
        return RestfulRegexUtils.Regex(uri,LongParttern,"{*}");
    }
}
